﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Linq.Expressions;

namespace Wicresoft.EKA.NRemedy.Linq
{
    public class ARSet<T> : IQueryable, IQueryable<T>, IEnumerable, IEnumerable<T>, IOrderedQueryable, IOrderedQueryable<T>
    {
        protected BaseQueryProvider baseQueryProvider;
        protected Expression expression;

        public ARSet()
        {
            this.baseQueryProvider = new ARQueryProvider<T>();
            this.expression = Expression.Constant(this);
        }

        public ARSet(BaseQueryProvider baseQueryProvider)
        {
            if (baseQueryProvider == null)
            {
                throw new ArgumentNullException("baseQueryProvider");
            }
            this.baseQueryProvider = baseQueryProvider;
            this.expression = Expression.Constant(this);
        }

        public ARSet(BaseQueryProvider baseQueryProvider, Expression expression1)
        {
            if (baseQueryProvider == null)
            {
                throw new ArgumentNullException("baseQueryProvider");
            }
            this.baseQueryProvider = baseQueryProvider;
            this.expression = expression1;
        }

        Expression IQueryable.Expression
        {
            get { return this.expression; }
        }

        Type IQueryable.ElementType
        {
            get { return typeof(T); }
        }

        IQueryProvider IQueryable.Provider
        {
            get { return this.baseQueryProvider; }
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return ((IEnumerable)this.baseQueryProvider.Execute(this.expression)).GetEnumerator();
        }

        public override string ToString()
        {
            return this.baseQueryProvider.GetQueryText(this.expression);
        }

        IEnumerator<T> IEnumerable<T>.GetEnumerator()
        {
            return ((IEnumerable<T>)this.baseQueryProvider.Execute(this.expression)).GetEnumerator();
        }
    }
}
